﻿package com.wsascb.assistant
{
	public class StringFormat
	{
		private static var num:Array = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"];
		private static var num2:Array = ["万","亿","兆"];
		//将阿拉伯数字转换为中文数字
		public static function numTcn(val:int):String
		{
			var cnnum:String;
			if (val<=10)
			{
				return num[val];
			}
			else
			{
				switch (val)
				{
					case 100 :
						cnnum = "佰";
						break;
					case 1000 :
						cnnum = "仟";
						break;
					case 10000 :
						cnnum = "万";
						break;
					case 100000000 :
						cnnum = "亿";
						break;
					default :
						cnnum = "";
				}
				return cnnum;
			}
		}
		//将数字日期转换为中文数字日期
		public static function cnDate(val:String):String
		{
			var lg:int = val.length;
			var dataArr:Array;
			var cndata:String;
			for (var i:int=0; i<lg; i++)
			{
				if (val.charAt(i) == "-")
				{
					dataArr = val.split("-");
					break;
				}
				else if (val.charAt(i)=="/")
				{
					dataArr = val.split("/");
					break;
				}
				else if (val.charAt(i)=="|")
				{
					dataArr = val.split("|");
					break;
				}
				else if (val.charAt(i)==".")
				{
					dataArr = val.split(".");
					break;
				}
			}
			if (dataArr==null)
			{
				dataArr = new Array(val);
			}
			cndata = cnNum(dataArr[0]) + cnNum(dataArr[1]) + cnNum(dataArr[2]);
			return cndata;
		}
		//阿拉伯数字转汉字字符
		public static function cnNum(val:String):String
		{
			var lg:int = val.length;
			var cn:String = "";
			for (var i:int=0; i<lg; i++)
			{
				cn +=  numTcn(int(val.charAt(i)));
			}
			return cn;
		}
		public static function cnNumInt(val:Number):String
		{
			var num:Number = val;
			var numStr:String = val.toString();
			var numLg:int = numStr.length;
			var cn:String = "";
			if (numLg>5&&numLg<9)
			{
				var sw:Number=int(num/10000);
				var gw:Number = num % (sw * 1000);
				cn +=  cnNumWan(sw) + "万" + cnNumWan(gw);
			}else if (numLg>=9)
			{
				var yw:Number=int(num/100000000);
				var ww:Number = num % (yw * 100000000);
				cn +=  cnNumInt(yw) + "亿" + cnNumInt(ww);
			}
			else
			{
				cn +=  cnNumWan(num);
			}
			return cn;
		}
		//万级别以下
		public static function cnNumWan(val:int):String
		{
			var numStr:String = val.toString();
			var numLg:int = numStr.length - 1;
			var cn:String = "";
			var ling:Boolean = false;
			if (val>=20)
			{
				for (var i:int=0; i<numLg; i++)
				{
					if (int(numStr.charAt(i))!=0)
					{
						ling = false;
						cn +=  numTcn(int(numStr.charAt(i))) + numTcn(Math.pow(10,numLg - i));
					}
					else
					{
						//前一位不等于0时添加数字"零"
						if (int(numStr.charAt(i-1))!=0)
						{
							cn +=  numTcn(int(numStr.charAt(i)));
							ling = true;
						}
						else
						{
							ling = true;
						}
					}
				}
			}
			else
			{
				//大于等于10小于20直接添加"拾"
				if (val>=10)
				{
					cn +=  "拾";
				}
			}
			//末尾数字不等于0添加
			if (int(numStr.charAt(numLg))!=0)
			{
				cn +=  numTcn(int(numStr.charAt(numLg)));
			}
			//去除末尾"零"字符
			if (ling)
			{
				cn = cn.substr(0,cn.length - 1);
			}
			return cn;
		}
	}
}